<template>
<view>
<block v-if="shops.length >0">
  <view class="store_list">
    <view class="store_item" v-for="(item, index) in shops" :key="index" :data-link="'near_shop_page.html?addShopId=' + item.id" @tap="tolinkUrl">
      <view class="store_item_left">
        <image class="store_item_logo" mode="widthFix" :src="item.shopLogo + '?x-oss-process=style/preview'" v-if="item.shopLogo"></image>
        <image class="store_item_logo" mode="widthFix" src="https://image1.sansancloud.com/xianhua/2020_1/2/18/36/36_931.jpg?x-oss-process=style/preview" v-else></image>
      </view>
      <view class="store_item_right">
        <text class="store_item_name">{{item.shopName}}</text>
        <view class="store_item_service">
          <text>服务时间：{{item.serviceStartTime}}:00-{{item.serviceEndTime}}:00</text>
          <text class="distance_text" v-if="item.distance">{{publicFun.toFix(item.distance)}}km</text>
          <text class="distance_text" v-else>未设置</text>
        </view>
        <scroll-view class="tags_content" scroll-x v-if="item.tagArray&&item.tagArray.length!=0">
          <text :style="'border:2rpx solid ' + defaultColor + ';color:' + defaultColor" class="tags_item" v-if="item.tagArray" v-for="(tags, index2) in item.tagArray" :key="index2">{{tags}}</text>
        </scroll-view>
        <view class="shop_content" v-else>
          <text>{{item.shopContent}}</text>
        </view>
      </view>
    </view>
  </view>
</block>
<block v-if="shops.length==0">
  <view class="noproduct">
    <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/16/15/54/55_950.jpg?x-oss-process=style/preview_120"></image>
    <text>没有相关店铺</text>
  </view>
</block>
</view>
</template>

<script>
import publicFun from '../../wxcomponents/public/publicFun.wxs'

export default {
  data() {
    return {
      page: "1",
      shops: null,
      journey: [],
      //公里数
      receiveData: {},
      curPage: 1,
      pageSize: 10,
      totalSize: 1,
      listPage: {
        page: 1,
        pageSize: 0,
        totalSize: 0,
        curpage: 1
      },
      setting: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.publicFun = publicFun;
    console.log("====options===", options);
    this.setData({
      receiveData: options
    });
    this.getData(this.listPage,2);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      setting: getApp().globalData.setting
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.reflesh == 1) {
      this.onPullDownRefresh();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.Data = [];
    this.listPage.page = 1;
    this.getData(this.listPage,2);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    if (that.listPage.totalSize > that.listPage.curPage * that.listPage.pageSize) {
      that.listPage.page++; // 如果跳到下一页把上一页的数据加进去

      // let shoparr = that.shops; //  现获取数据，当翻页后把上一页的内容加进去

      this.getData(that.listPage);

      // if (that.page > 1) {
      //   shoparr = shoparr.concat(that.shops);
      //   that.setData({
      //     shops: shoparr
      //   });
      // }
    }
  },
  methods: {
    tolinkUrl: function (e) {
      console.log("e.currentTarget.dataset.link=====", e.currentTarget.dataset.link);
      let linkUrl = e.currentTarget.dataset.link;
      getApp().globalData.linkEvent(linkUrl);
    },
    // 获取附近店铺数据
    getData: function (params,ifAdd) {
      let that = this; // 店铺名可以从app.setting中拿到
      if (!ifAdd) {
        ifAdd = 1;
      }
      console.log("app.setting.platformSetting", getApp().globalData.setting);
      let shopName = getApp().globalData.setting.platformSetting.defaultShopBean.account.shopName;
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          let latitude = res.latitude;
          let longitude = res.longitude;
          console.log(latitude);
          console.log(params); // 获取附近店铺数据

          let nearShopUrl = "/more_near_shops.html";
          let pageParam = {
            "longitude": longitude,
            "latitude": latitude,
            //  "shopTag":"",
            "page": params.page
          };
          pageParam = Object.assign({}, pageParam, that.receiveData);
          console.log(nearShopUrl + pageParam);
          let customIndex = getApp().globalData.AddClientUrl(nearShopUrl, pageParam, 'get', 1); 
          getApp().globalData.showToastLoading('loading', true); //拿custom_page
          wx.request({
            url: customIndex.url,
            header: getApp().globalData.header,
            method: 'GET',
            success: function (res) {
              console.log("数据", res.data.relateObj);
              // let shops = res.data.relateObj.result; // 获取公里数
              let dataArr = that.shops;
              that.listPage.pageSize = res.data.relateObj.pageSize;
              that.listPage.curPage = res.data.relateObj.curPage;
              that.listPage.totalSize = res.data.relateObj.totalSize;
              for (let i = 0; i < res.data.relateObj.result.length; i++) {
                res.data.relateObj.result[i].distance = getApp().globalData.getDistance(latitude, longitude, res.data.relateObj.result[i].latitude, res.data.relateObj.result[i].longitude);
              }
              if (ifAdd == 2) {
                dataArr = [];
              }
              
              if (!res.data.relateObj.result || res.data.relateObj.result.length == 0) {
                that.setData({
                  shops: null
                });
              } else {
                if (dataArr == null) {
                  dataArr = [];
                }
              
                dataArr = dataArr.concat(res.data.relateObj.result);
                that.setData({
                  shops: dataArr
                });
              }
              // that.setData({
              //   shops: shops,
              //   curPage: res.data.relateObj.curPage,
              //   pageSize: res.data.relateObj.pageSize,
              //   totalSize: res.data.relateObj.totalSize
              // });

              if (res.data.errcode < 0) {
                console.log(res.data.errMsg);
              } else {
                wx.hideLoading();

                // if (!!res.data.partials) {
                //   that.getPartials(res.data.partials);
                // } else {
                //   console.log('--------error --------' + res.data);
                // }
              }
            },
            fail: function (res) {
              wx.hideLoading();
              getApp().globalData.loadFail();
            }
          });
        }
      });
    },
    // 
    getGreatCircleDistance: function (lng1, lat1, lng2, lat2) {
      var EARTH_RADIUS = 6378.137; //地球半径

      lng1 = parseFloat(lng1);
      lat1 = parseFloat(lat1);
      lng2 = parseFloat(lng2);
      lat2 = parseFloat(lat2);
      console.log("a,b", lng1, lat1, lng2, lat2);
      var radLat1 = lat1 * Math.PI / 180.0;
      var radLat2 = lat2 * Math.PI / 180.0;
      var a = radLat1 - radLat2;
      var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
      s = s * EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000.0; // console.log("公里数",s)
      // console.log("公里数", this.data.journey)

      s = s.toFixed(1);
      var journey = this.journey;
      journey.push(s);
      this.setData({
        journey: journey
      }); // console.log("公里数", this.data.journey)

      return s;
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>